package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    NursingLevelMapper nursingLevelMapper;

    @Override
    public List<NursingPlanVO> selectAllPlan() {

        List<NursingPlanVO> nursingPlanVOS = nursingPlanMapper.selectAllPlan();

        List<Long> list = nursingPlanVOS.stream().map(e -> e.getId()).collect(Collectors.toList());

        List<Map> listMap = nursingLevelMapper.selectLevelCountByPlanIds(list);
        Map<Long, Long> resultMap = listMap.stream()
                .collect(Collectors.toMap(
                        map -> (Long) map.get("planId"),
                        map -> (Long) map.get("count")
                ));

        nursingPlanVOS.forEach(e -> {
            Integer count = resultMap.getOrDefault(e.getId(), 0L).intValue();
            e.setCount(count);
        });

        return nursingPlanVOS;
    }

    /**
     * 新增护理计划
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(0);
        nursingPlanMapper.addNursingPlan(nursingPlan);

        List<NursingProjectPlan> list = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(e -> {
                e.setPlanId(nursingPlan.getId());
            });
            nursingProjectPlanMapper.insertNursingProjectPlans(list);
        }
    }

    /**
     * 分页条件查询护理项目
     */
    @Override
    public PageResponse selectByPage(String name, Integer status, Integer pageNum, Integer pageSize) {

        //设置分页
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVO> nursingPlanVOS = nursingPlanMapper.selectByPage(name, status);

        List<Long> list = nursingPlanVOS.stream().map(e -> e.getId()).collect(Collectors.toList());

        List<Map> listMap = nursingLevelMapper.selectLevelCountByPlanIds(list);
        Map<Long, Long> resultMap = listMap.stream()
                .collect(Collectors.toMap(
                        map -> (Long) map.get("planId"),
                        map -> (Long) map.get("count")
                ));

        nursingPlanVOS.forEach(e -> {
            Integer count = resultMap.getOrDefault(e.getId(), 0L).intValue();
            e.setCount(count);
        });

        return PageResponse.of(nursingPlanVOS, NursingPlanVO.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(NursingPlanDto nursingPlanDto, Long id) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);

        nursingPlanMapper.update(nursingPlan);

        // 删除之前的记录
        nursingProjectPlanMapper.deleteByPlanId(id);

        // 重新添加记录
        List<NursingProjectPlan> list = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(e -> {
                e.setPlanId(id);
            });
            nursingProjectPlanMapper.insertNursingProjectPlans(list);
        }
    }

    @Override
    public NursingPlanVO getNursingPlan(Long id) {
        return nursingPlanMapper.getNursingPlan(id);
    }

    /**
     * 根据id删除护理计划
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        nursingPlanMapper.deleteById(id);
    }

    /**
     * 启动和禁用状态status
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id, status);
    }
}
